﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StateGrid
{   
    /// <summary>
    /// 测试
    /// </summary>
    class Program
    {
        static UserManageDAO dao = new UserManageDAO();

        static void Main(string[] args)
        {
            TestInsertAndDeleteUser();
            TestDifferentGetUserAPIs();
            TestUpdateUser();
        }

        static void TestDifferentGetUserAPIs()
        {
            Console.WriteLine("--------------------------------");
            Console.WriteLine("测试各种从数据库中获取用户的API：");
            //用户总数
            Console.WriteLine("Users Count:" + dao.CountUsers());

            //测试各种排序
            IList<User> users = dao.GetUsers(0, 1000, UserManageDAO.UserOrderType.BY_NAME);
            Console.WriteLine();
            Console.WriteLine("All users order by name:");
            foreach (User u in users)
                OutputUser(u);
            Console.WriteLine();
            Console.WriteLine("All users order by ip:");
            users = dao.GetUsers(0, 1000, UserManageDAO.UserOrderType.BY_IP);
            foreach (User u in users)
                OutputUser(u);
            Console.WriteLine();
            Console.WriteLine("All users order by role:");
            users = dao.GetUsers(0, 1000, UserManageDAO.UserOrderType.BY_ROLE);
            foreach (User u in users)
                OutputUser(u);

            //测试选取某个范围的结果集
            Console.WriteLine();
            Console.WriteLine("Select 2nd user order by name(skip 1st result and result size set to 2):");
            users = dao.GetUsers(1, 2, UserManageDAO.UserOrderType.BY_NAME);
            foreach (User u in users)
                OutputUser(u);
        }

        static void OutputUser(User u) {
            if (u == null)
            {
                Console.WriteLine("用户不存在");
                return;
            }
            String outputString = u.name + " " + u.password + " " + u.ipLimit + " " + u.role + "=>";
            if (u.grantedModulars != null)
            {
                foreach (FunctionModular mod in u.grantedModulars)
                {
                    outputString += " " + mod.name;
                }
            }
            Console.WriteLine(outputString);
        }

        static void TestInsertAndDeleteUser()
        {
            Console.WriteLine("--------------------------------");
            Console.WriteLine("测试添加和删除用户：");
            //新增一个用户
            String username = "TestUserDelete";
            Console.WriteLine("添加TestUserDelete...");
            List<String> functionIds = new List<String>();
            functionIds.Add("zhongwenqikan");
            functionIds.Add("zhongwenhuiyi");
            functionIds.Add("guojiabiaozhun");
            dao.AddUser(username, "123456", "127.0.0.*", UserRole.NORMAL, functionIds);

            Console.WriteLine("检索TestUserDelete：");
            User user = dao.GetUserByName(username);
            OutputUser(user);

            Console.WriteLine("删除TestUserDelete...");
            dao.DeleteUserByName(username);

            Console.WriteLine("重新检索TestUserDelete：");
            user = dao.GetUserByName(username);
            OutputUser(user);
        }

        static void TestUpdateUser()
        {
            Console.WriteLine("--------------------------------");
            Console.WriteLine("测试修改用户wangleye：");
            Console.WriteLine("修改前：");
            User oldUser = dao.GetUserByName("wangleye");
            OutputUser(oldUser);
            
            //进行修改
            List<String> functionIds = new List<String>();
            functionIds.Add("zhongwenqikan");
            functionIds.Add("zhongwenhuiyi");
            dao.UpdateUserByName("wangleye", "changepassword", "11.11.11.11", UserRole.EDITOR, functionIds);
            
            Console.WriteLine("修改后：");
            OutputUser(dao.GetUserByName("wangleye"));

            //恢复
            Console.WriteLine("恢复：");
            dao.UpdateUserByName(oldUser);
            OutputUser(dao.GetUserByName("wangleye"));

        }
    }
}
